package com.xilihui.xlh.core.util;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 2017/2/20.
 */

public class TextExtend {

    public static SpannableStringBuilder getSalePrice(String str, int color) {
        int from = 0;
        int to = 0;
        Pattern p = Pattern.compile("[\\d]+.?[\\d]*");// 匹配数字（整数和浮点数）
        Matcher matcher = p.matcher(str);
        if (matcher.find()) {
            from = matcher.start();
            to = matcher.end();
        }
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(color), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new RelativeSizeSpan(2f), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }

    public static SpannableStringBuilder getMultiColorText(String str, int color) {
        int from = 0;
        int to = str.length();
        if (str.contains("：")) {
            from = str.lastIndexOf("：") + 1;
        } else if (str.contains(" ")) {
            from = str.lastIndexOf(" ") + 1;
        }
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        style.setSpan(new ForegroundColorSpan(color), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }

    public static SpannableStringBuilder getMultiSizeText(String str) {
        int from;
        int to = str.length();
        if (str.contains(".")) {
            from = str.lastIndexOf(".");
        } else if (str.contains("万")) {
            from = str.lastIndexOf("万");
        } else if (str.contains("元")) {
            from = str.lastIndexOf("元");
        } else if (str.contains("/")) {
            from = str.lastIndexOf("/");
        } else {
            from = str.length() - 1;
        }
        SpannableStringBuilder style = new SpannableStringBuilder(str);
//        style.setSpan(new AbsoluteSizeSpan(14, true), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new RelativeSizeSpan(0.7f), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (str.startsWith("¥")) {
            style.setSpan(new RelativeSizeSpan(0.7f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return style;
    }

    public static SpannableStringBuilder getMultiSizeText1(String str) {
       
        SpannableStringBuilder style = new SpannableStringBuilder(str);
        if (str.startsWith("¥")) {
            style.setSpan(new RelativeSizeSpan(0.7f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return style;
    }

    public static SpannableStringBuilder getCountDownText(String str) {


        String s = str.replace("天", "天\n").replace("分", "分\n");

        int from = 0;
        int to = s.length();
        if (s.contains("天")) {
            from = s.lastIndexOf("天");
        }
        SpannableStringBuilder style = new SpannableStringBuilder(s);
        style.setSpan(new RelativeSizeSpan(0.8f), from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }

    /**
     * 手机号码正则判断
     *
     * @param tel
     * @return
     */
    public static boolean isMobile(String tel) {

        if (TextUtils.isEmpty(tel)) {
            return false;
        }

        String regExp = "^(0|86|17951)?(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[57])[0-9]{8}$";

        Pattern p = Pattern.compile(regExp);

        Matcher m = p.matcher(tel);

        return m.find();
    }

    /**
     * 固定电话正则判断
     *
     * @param str
     * @return
     */
    public static boolean isPhone(String str) {
        Pattern p1, p2;
        Matcher m;
        boolean b;
        p1 = Pattern.compile("^(010|02[0-9]|0[3456789][1-9][0-9])[0-9]{5,10}$");  // 验证带区号的
        p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");        // 验证没有区号的
        if (str.length() > 9) {
            m = p1.matcher(str);
            b = m.matches();
        } else {
            m = p2.matcher(str);
            b = m.matches();
        }
        return b;
    }

    public static boolean isNumber(String number) {
        if (TextUtils.isEmpty(number)) {
            return false;
        }

        if (TextUtils.isDigitsOnly(number)) {
            return true;
        }

        String regExp = "[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+";

        Pattern p = Pattern.compile(regExp);

        Matcher m = p.matcher(number);

        return m.find();
    }

    public static boolean isNotMobile(String tel) {
        return !isMobile(tel);
    }

    public static String formatStorage(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;

        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else
            return String.format("%d B", size);
    }

    // 判断是否符合身份证号码的规范
    public static boolean isIDCard(String IDCard) {
        if (IDCard != null) {
            String IDCardRegex = "(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x|Y|y)$)";
            return IDCard.matches(IDCardRegex);
        }
        return false;
    }

    public static boolean isNotIDCard(String IDCard) {
        return !isIDCard(IDCard);
    }

    public static boolean isNotBankCard(String cardId) {
        return !isBankCard(cardId);
    }

    /**
     * 判断是否是银行卡号
     *
     * @param cardId
     * @return
     */
    public static boolean isBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
        if (bit == 'N') {
            return false;
        }
        return cardId.charAt(cardId.length() - 1) == bit;

    }

    private static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            // 如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    /**
     * 设置textview部分颜色 可以设置多种颜色
     *
     * TextStringUtils.setSpanTextView(tv_message,"ysjjobssdfsadfsadfas",new TextStringUtils.SpanColorHolder(3,8,getResources().getColor(R.color.blue)),
     new TextStringUtils.SpanColorHolder(7,10,getResources().getColor(R.color.green)));
     * @param tv
     * @param str
     * @param colorHolders
     */
    public static void setSpanTextView(TextView tv, String str,
                                       SpanColorHolder... colorHolders) {

        if (colorHolders == null || colorHolders.length == 0) {
            tv.setText(str);
            return;
        }
        SpannableStringBuilder builder = new SpannableStringBuilder(str);

        for (int i = 0; i < colorHolders.length; i++) {
            SpanColorHolder holder = colorHolders[i];
            ForegroundColorSpan span = new ForegroundColorSpan(holder.color);
            builder.setSpan(span, holder.start, holder.end,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }
        tv.setText(builder);
        // ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
    }



    public static class SpanColorHolder {
        int start;
        int end;
        int color;

        public SpanColorHolder(int start, int end, int color) {
            super();
            this.start = start;
            this.end = end;
            this.color = color;
        }
    }
    
}
